package com.rovinj_server.utisl;

import com.rovinj_server.entity.People;

import java.io.File;
import java.io.IOException;
import java.util.List;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;


/**
 * Created by root on 8/13/16.
 */
public class ExcelUtils {
    public static void write(File file, List<People> peoples) throws IOException, WriteException {
        if(!file.exists())
            file.createNewFile();

        WritableWorkbook workbook = Workbook.createWorkbook(file);
        WritableSheet sheet = workbook.createSheet("Sensus Warga", 0);
        try {

            /**
             * new Label(0 , 0, Nama)
             * membuat teks "Nama" di kolom pertama baris ke pertama
             * angka pertama menunjukkan kolom
             * angka kedua menunjukkan baris
             * "库位", "条码", "款式", "实际数", "多于", "差异", "盘点数", "实际数总数", "多于数总数", "差异数总数", "盘点数总数"
             */

            sheet.addCell(new Label(0, 0, "location"));
            sheet.addCell(new Label(1, 0, "barode"));
            sheet.addCell(new Label(2, 0, "code"));
            sheet.addCell(new Label(3, 0, "sjnumber"));
            sheet.addCell(new Label(4, 0, "moreThan"));
            sheet.addCell(new Label(5, 0, "lessThan"));
            sheet.addCell(new Label(6, 0, "discNum"));
            sheet.addCell(new Label(7, 0, "sjnumberZong"));
            sheet.addCell(new Label(8, 0, "moreThanZong"));
            sheet.addCell(new Label(9, 0, "lessThanZong"));
            sheet.addCell(new Label(10, 0, "discNumberZong"));

            for (int i = 0; i < peoples.size(); i++){
                People people = peoples.get(i);
                sheet.addCell(new Label(0, i, people.getLocation()));
                sheet.addCell(new Label(1, i, people.getBarode()));
                sheet.addCell(new Label(2, i, people.getCode()));
                sheet.addCell(new Label(3, i, people.getSjnumber()));
                sheet.addCell(new Label(4, i, people.getMoreThan()));
                sheet.addCell(new Label(5, i, people.getLessThan()));
                sheet.addCell(new Label(6, i, people.getDiscNum()));
                sheet.addCell(new Label(7, i, people.getSjnumberZong()));
                sheet.addCell(new Label(8, i, people.getMoreThanZong()));
                sheet.addCell(new Label(9, i, people.getLessThanZong()));
                sheet.addCell(new Label(10, i, people.getDiscNumberZong()));
            }
        } catch (WriteException e) {
            e.printStackTrace();
        }

        workbook.write();
        workbook.close();

    }
}
